<?php

class News_NewsController extends Zend_Controller_Action {
	
	public function indexAction() {
		
		$news = Doctrine_Core::getTable('News_Model_News')->findAll();
		
		$this->view->news = $news;
	}
	
	public function createAction() {
		$form = new News_Form_News();
		$form->setAction($this->view->url(array(),'news_news_create'));
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			$news = new News_Model_News();
			//Zend_Debug::dump($values);
			foreach ($values['name'] as $lang=>$value) {
				$news->Translation[$lang]->name = $value;	
			}
			foreach ($values['anonce'] as $lang=>$value) {
				$news->Translation[$lang]->anonce = $value;	
			}
			foreach ($values['content'] as $lang=>$value) {
				$news->Translation[$lang]->content = $value;	
			}
			foreach ($values['description'] as $lang=>$value) {
				$news->Translation[$lang]->description = $value;	
			}
			foreach ($values['keywords'] as $lang=>$value) {
				$news->Translation[$lang]->keywords = $value;	
			}
			$news->published = $values['published'];
			$news->save();
			$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('News has been save successfully!'));
			return $this->_redirect($this->view->url(array(),'news_news_index'));
			
		}
		$this->view->form = $form;
	}
	
	public function updateAction() {
		
		$id = $this->_request->getParam('id', 0);
		
		$news = Doctrine_Core::getTable('News_Model_News')->find($id);
		
		if(!$id || !$news) {
			throw new Zend_Controller_Action_Exception('Request news not found', 404);	
		}
		
		$form = new News_Form_News();
		$form->setAction($this->view->url(array('id'=>$id),'news_news_update'));
		
		$populate = array('published' => $news->published);
		
		$locales = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOption('locales');
		
		foreach ($locales as $lang=>$value) {
			$populate['name'][$lang] = $news->Translation[$lang]->name;
			$populate['anonce'][$lang] = $news->Translation[$lang]->anonce;
			$populate['content'][$lang] = $news->Translation[$lang]->content;
			$populate['description'][$lang] = $news->Translation[$lang]->description;
			$populate['keywords'][$lang] = $news->Translation[$lang]->keywords;
		}
		
		$this->view->form = $form;
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			foreach ($values['name'] as $lang=>$value) {
				$news->Translation[$lang]->name = $value;	
			}
			foreach ($values['anonce'] as $lang=>$value) {
				$news->Translation[$lang]->anonce = $value;	
			}
			foreach ($values['content'] as $lang=>$value) {
				$news->Translation[$lang]->content = $value;	
			}
			foreach ($values['description'] as $lang=>$value) {
				$news->Translation[$lang]->description = $value;	
			}
			foreach ($values['keywords'] as $lang=>$value) {
				$news->Translation[$lang]->keywords = $value;	
			}
			$news->published = $values['published'];
			$news->updated_at = time();
			$news->save();
			$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('News item updated successfully!'));
				 
			$this->_redirect($this->view->url(array(),'news_news_index'));
		} else {
			$form->populate($populate);
		}
		
	}
	
	public function deleteAction() {
		$id = $this->_request->getParam('id', 0);
		
		$news = Doctrine_Core::getTable('News_Model_News')->find($id);
		
		if(!$id || !$news) {
			throw new Zend_Controller_Action_Exception('Request news not found', 404);		
		}
		
		$news->delete();
		
		$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('News item deleted successfully!'));
				 
		$this->_redirect($this->view->url(array(),'news_news_index'));
	}
}